package net.devkit.common.toolkit;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import net.devkit.common.toolkit.date.DateUtils;

import org.springframework.web.context.ContextLoader;

/**
 * PM工具类,(工具类全为静态方法)声明为抽象以达到不可new　实例化的效果
 * @author shizeyuan
 *
 */
public abstract class DevTool {
	
	/**
	 * 获取Spting上下文bean对象
	 * @return
	 */
	public static <T> T obtainSpringBean(String beanname, Class<T> clazz){
		return ContextLoader.getCurrentWebApplicationContext().getBean(beanname, clazz);
	}
	

	/**
	 * 清空session
	 * @param session
	 */
	public static void clearSession(HttpSession session) {
		if (session == null)
			return;
		final Enumeration<String> em = session.getAttributeNames();
		while (em.hasMoreElements()){
			final String key = em.nextElement();
			session.removeAttribute(key);
		}
	}
	
	/**
	 * 对象是否为空
	 * @param obj
	 * @return
	 */
	public static Boolean isEmpty(Object obj){
		if(obj == null)
			return true;
		return false;
	}
	
	/**
	 * 对象是否不为空
	 * @param obj
	 * @return
	 */
	public static Boolean isNotEmpty(Object obj){
		return !isEmpty(obj);
	}
	
	
	/**生成唯一编号
	 * @return
	 */
	public static final synchronized String generateUniqueno() {
		final StringBuilder sb = new StringBuilder();
		final char[] tempnowtimestampchars = DateUtils.obtainCurrentStrdate(DateUtils.FORMAT_FULL).substring(2).toCharArray();
		for (char c : tempnowtimestampchars) {
			if (c == '-' || c == ' ' || c == ':' || c == '.')
				continue;
			sb.append(c);
		}
		final int len = 20 - sb.length();
		int i = 1;
		for (; i <= len; i++) {
			sb.append(String.valueOf(Math.random()*100).charAt(0));
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		List<String> strlist = new ArrayList<String>();
		for(int i=0;i<100011;i++){
			String s = DevTool.generateUniqueno();
			if(strlist.contains(s)){
				System.err.println(s);
				break;
			}
			System.out.println(s);
			System.out.println(s.length());
			strlist.add(s);
			
		}
	}
	/**
	 * 生成加密算法的盐值(用户登录使用)　
	 * @return
	 */
	public static final String generateSaltValue() {
		int salf_leg = DevConstant.SALF_LENGTH;

		final char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
				'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
				'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
				'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
				'W', 'X', 'Y', 'Z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		int maxlen = str.length;

		StringBuilder salf = new StringBuilder();
		Random random = new Random();
		int c = 0;
		for (; c < salf_leg; c++) {
			salf.append(str[Math.abs(random.nextInt(maxlen))]);
		}
		return salf.toString();
	}
	
	/**
	 * 是否包含,拥有该权限
	 * 
	 * @return
	 */
	public static boolean isHasPower(String code, List<String> powers) {
		for (String power : powers) {
			if (power.equals(code)) {
				return true;
			}
		}
		return false;
	}
	
	
}
